import "mod_draw";
import "mod_key";
import "mod_map";
import "mod_proc";
import "mod_grproc";
import "mod_screen";
import "mod_text";
import "mod_file";
import "mod_video";
import "mod_string";
import "mod_debug";
import "mod_say";

//
// Ejemplo extraido desde Bennu WIKI
//

Declare Function String textinput();
End

// inicio del programa
BEGIN

	// inicializacion de video
	scale_mode = SCALE_NOFILTER;
	set_mode(320, 240, 16, MODE_WINDOW + MODE_WAITVSYNC);
	set_fps(60,0);
	
    say("You typed: '" + textinput() + "'");
	
	loop
		IF ( key ( _esc ) )
			exit();
		END
	
		frame;
	end
	
END


Function String textinput()
 Private
    String str;
    int t;
    int t2;
    byte last_ascii;
    int txtid;
 Begin

    // show what you type in top left corner
    txtid = write_string(0,0,0,0,&str);

    // clean the string
    str = "";

    // get input from the user
    // pressing enter will end the loop
    Loop
        if(ascii!=0&&last_ascii==ascii) // check if a key is pressed and if the same key
                                        // was pressed last frame
            if(t==0||t>fps/4) // check if the key was just pressed or it has been pressed
                              // for 0.25 seconds
                if(t==0||t2>fps/30) // check if the key was just pressed or it has been pressed
                                    // for the last 0.03 seconds
                    t2=0;
                    switch(ascii) // handle input
                        case 8: //backspace
                            str = substr(str,0,len(str)-1);
                        end
                        case 13: //enter
                            break;
                        end
                        default: //addkey
                            str+=chr(ascii);
                        end
                    end
                end
                t2++;
            end
            t++;
        else
            t = t2 = 0; // reset
        end
        last_ascii = ascii;
        frame;
    End

    // delete the text used
    delete_text(txtid);

    // return the typed string
    return str;

 End

